﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Drawing;
using System.Windows.Forms;
using HYCreateDuct.MethodCreateDuct;

namespace HYCreateDuct.MethodHelp
{
	internal class SqlLiteHelp : Database
	{
		public static void SqlLiteDatabase(string ConnStr)
		{
			Database.dbCon = new SQLiteConnection("Data Source=" + ConnStr);
			if (Database.dbCon.State == ConnectionState.Closed)
			{
				Database.dbCon.Open();
			}
		}

		public static DuctParam ReadDuctParam()
		{
			DuctParam ductParam = new DuctParam();
			DataTable dataTable = Database.ExecQuery("select *from Duct_ParamCompute order by id ");
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				ductParam.dTemperature = double.Parse(dataTable.Rows[i]["Temperature"].ToString());
				ductParam.dPress = double.Parse(dataTable.Rows[i]["Press"].ToString());
				ductParam.dDensity = double.Parse(dataTable.Rows[i]["Density"].ToString());
				ductParam.dHumidity = double.Parse(dataTable.Rows[i]["Humidity"].ToString());
				ductParam.dViscosity = double.Parse(dataTable.Rows[i]["Viscosity"].ToString());
			}
			return ductParam;
		}

		public static int UpdateDuctParam(DuctParam param)
		{
			return Database.ExecuteCommand(string.Format("UPDATE Duct_ParamCompute SET Density={0:G}, Humidity={1:G},Press={2:G},Temperature={3:G},Viscosity={4:G}", new object[]
			{
				param.dDensity,
				param.dHumidity,
				param.dPress,
				param.dTemperature,
				param.dViscosity
			}));
		}

		public static int UpdateCompute(int Type, double min, double max, string RGBs)
		{
			return Database.ExecuteCommand(string.Format("UPDATE Duct_Compute SET RGB='{0:G}'  where Type={1:G} and Min={2:G} and Max={3:G}", new object[]
			{
				RGBs,
				Type,
				min,
				max
			}));
		}

		public static List<DuctRectSize> GetRectSize()
		{
			List<DuctRectSize> list = new List<DuctRectSize>();
			DataTable dataTable = Database.ExecQuery("select *from Duct_RectSize order by Width asc, Height asc ");
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				list.Add(new DuctRectSize
				{
					dWidth = double.Parse(dataTable.Rows[i]["Width"].ToString()),
					dHeight = double.Parse(dataTable.Rows[i]["Height"].ToString())
				});
			}
			return list;
		}

		public static List<double> GetCircleSize()
		{
			List<double> list = new List<double>();
			DataTable dataTable = Database.ExecQuery("select *from Duct_CircleSize order by Dimeter ");
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				double item = double.Parse(dataTable.Rows[i]["Dimeter"].ToString());
				list.Add(item);
			}
			return list;
		}

		public static List<MaterialAndRough> GetMaterialAndRough()
		{
			List<MaterialAndRough> list = new List<MaterialAndRough>();
			DataTable dataTable = Database.ExecQuery("select *from Duct_Material order by id ");
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				list.Add(new MaterialAndRough
				{
					strMaterial = dataTable.Rows[i]["material"].ToString(),
					dRange = double.Parse(dataTable.Rows[i]["range"].ToString()),
					dSetValue = double.Parse(dataTable.Rows[i]["setup"].ToString())
				});
			}
			return list;
		}

		public static int DeleteDuctSize(string width, string height)
		{
			int result;
			try
			{
				if (!string.IsNullOrEmpty(height))
				{
					int num = int.Parse(width);
					int num2 = int.Parse(height);
					result = Database.ExecuteCommand(string.Format("delete from  Duct_RectSize where Width={0:G} and Height={1:G}", num, num2));
				}
				else
				{
					int num3 = int.Parse(width);
					result = Database.ExecuteCommand(string.Format("delete from  Duct_CircleSize where Dimeter={0:G}", num3));
				}
			}
			catch
			{
				result = 0;
			}
			return result;
		}

		public static int DeleteMaterialAndRough(string strMaterial)
		{
			return Database.ExecuteCommand(string.Format("delete from  Duct_Material where material='{0:G}'", strMaterial));
		}

		public static int DeleteTableData(string tableName)
		{
			return Database.ExecuteCommand(string.Format("delete from  {0:G} ", tableName));
		}

		public static int DeleteTableData(int Type, double min, double max)
		{
			string text = "Duct_Compute";
			return Database.ExecuteCommand(string.Format("delete from  {0:G}  where Type={1:G} and min={2:G} and max={3:G}", new object[]
			{
				text,
				Type,
				min,
				max
			}));
		}

		public static int DeleteTableData(string tableName, int Type)
		{
			return Database.ExecuteCommand(string.Format("delete from  {0:G}  where Type={1:G}", tableName, Type));
		}

		public static int InsertDuctSize(int dWidth, int dHeight)
		{
			int result;
			try
			{
				if (dHeight != 0)
				{
					DataTable dataTable = Database.ExecQuery(string.Concat(new object[]
					{
						"select *from Duct_RectSize where Width=",
						dWidth,
						"  and Height=",
						dHeight,
						"   order by id "
					}));
					if (dataTable != null && dataTable.Rows.Count > 0)
					{
						result = -1;
					}
					else
					{
						result = Database.ExecuteCommand(string.Format("insert   into   Duct_RectSize (Width,Height) values ({0:G},{1:G})", dWidth, dHeight));
					}
				}
				else
				{
					DataTable dataTable2 = Database.ExecQuery("select *from Duct_RectSize where Dimeter=" + dWidth + "   order by id ");
					if (dataTable2 != null && dataTable2.Rows.Count > 0)
					{
						result = -1;
					}
					else
					{
						result = Database.ExecuteCommand(string.Format("insert   into  Duct_CircleSize  (Dimeter) values ({0:G})", dWidth));
					}
				}
			}
			catch
			{
				result = 0;
			}
			return result;
		}

		public static int AddMaterialAndRough(string strMaterial, string range, string setup)
		{
			int result;
			try
			{
				if (Database.ExecQuery("select *from  Duct_Material where material='" + strMaterial + "'").Rows.Count > 0)
				{
					result = -1;
				}
				else
				{
					double num = double.Parse(range);
					double num2 = double.Parse(setup);
					result = Database.ExecuteCommand(string.Format("insert into Duct_Material (material,range,setup) values('{0:G}',{1:G},{2:G}) ", strMaterial, num, num2));
				}
			}
			catch
			{
				result = 0;
			}
			return result;
		}

		public static int AddSpeed(double min, double max, string strRGB, int Type)
		{
			int result;
			try
			{
				if (Database.ExecQuery(string.Concat(new object[]
				{
					"select *from Duct_Compute where  Min=",
					min,
					" and Max=",
					max,
					" and Type=",
					Type
				})).Rows.Count > 0)
				{
					result = -1;
				}
				else
				{
					result = Database.ExecuteCommand(string.Format("insert into Duct_Compute (Min,Max,RGB,Type) values('{0:G}',{1:G},'{2:G}',{3:G}) ", new object[]
					{
						min,
						max,
						strRGB,
						Type
					}));
				}
			}
			catch
			{
				result = 0;
			}
			return result;
		}

		public static void UpdataDuctMaterial(DataGridView dt)
		{
			if (dt == null || dt.Rows.Count == 0)
			{
				return;
			}
			Database.ExecuteCommand("delete from Duct_Material ");
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				string text = dt[0, i].Value.ToString();
				string text2 = dt[1, i].Value.ToString();
				string text3 = dt[2, i].Value.ToString();
				if (!string.IsNullOrEmpty(text2) && !string.IsNullOrEmpty(text3) && !string.IsNullOrEmpty(text))
				{
					double num = double.Parse(text2);
					double num2 = double.Parse(text3);
					if (num == 0.0 && num2 == 0.0)
					{
						num = 0.0;
						num2 = 1.0;
					}
					Database.ExecuteCommand(string.Format("insert into Duct_Material (material,range,setup) values('{0:G}',{1:G},{2:G}) ", text, num, num2));
				}
			}
		}

		public static void UpdateDuctCompute(DataGridView dt, int Type)
		{
			if (dt == null || dt.Rows.Count == 0)
			{
				return;
			}
			Database.ExecuteCommand("delete from Duct_Compute where Type=" + Type);
			for (int i = 0; i < dt.Rows.Count; i++)
			{
				string text = dt[0, i].Value.ToString();
				string text2 = dt[1, i].Value.ToString();
				string text3 = dt[2, i].Value.ToString();
				if (!string.IsNullOrEmpty(text) && !string.IsNullOrEmpty(text2))
				{
					System.Drawing.Color xmldataColor = GetDuctPara.getXMLDataColor(text3);
					double.Parse(text);
					double.Parse(text2);
					if ((!text.Equals("0") || !text2.Equals("0")) && !string.IsNullOrEmpty(text3))
					{
						string text4 = string.Format("{0:G},{1:G},{2:G}", xmldataColor.R, xmldataColor.G, xmldataColor.B);
						Database.ExecuteCommand(string.Format("insert into Duct_Compute (Min,Max,RGB,Type) values('{0:G}',{1:G},'{2:G}',{3:G}) ", new object[]
						{
							text,
							text2,
							text4,
							Type
						}));
					}
				}
			}
		}

		public static List<DuctCompute> GetCompute(int Type)
		{
			List<DuctCompute> list = new List<DuctCompute>();
			DataTable dataTable = Database.ExecQuery(string.Format("select *from Duct_Compute  where type={0:G} order by id ", Type));
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				list.Add(new DuctCompute
				{
					dMin = double.Parse(dataTable.Rows[i]["Min"].ToString()),
					dMax = double.Parse(dataTable.Rows[i]["Max"].ToString()),
					RGB = DuctCompute.GetRGB(dataTable.Rows[i]["RGB"].ToString())
				});
			}
			return list;
		}

		public static List<string> GetParaDuctSystemType(string SystemTypeName, List<MaterialAndRough> lstM)
		{
			List<string> list = new List<string>();
			list.Add("中低压");
			list.Add("镀锌钢板");
			DataTable dataTable = Database.ExecQuery(string.Format("select *from Duct_SystemType  where SystemTypeName='{0:G}' order by id ", SystemTypeName));
			if (dataTable == null || dataTable.Rows.Count != 1)
			{
				list.Clear();
				list.Add("中低压");
				list.Add(lstM[0].strMaterial);
				return list;
			}
			for (int i = 0; i < dataTable.Rows.Count; i++)
			{
				string text = dataTable.Rows[i]["PressLevel"].ToString();
				string text2 = dataTable.Rows[i]["DuctMaterial"].ToString();
				if (string.IsNullOrEmpty(text) || string.IsNullOrEmpty(text2))
				{
					break;
				}
				list.Clear();
				using (List<MaterialAndRough>.Enumerator enumerator = lstM.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						if (enumerator.Current.strMaterial == text2)
						{
							list.Clear();
							list.Add(text);
							list.Add(text2);
							break;
						}
					}
				}
				if (list.Count == 0)
				{
					list.Add(text);
					list.Add(lstM[0].strMaterial);
				}
			}
			return list;
		}

		public static void InsertDuctSystemType(List<DuctSytemSet> lstType)
		{
			Database.ExecuteCommand("delete from Duct_SystemType");
			foreach (DuctSytemSet ductSytemSet in lstType)
			{
				if (ductSytemSet.strLineType == null)
				{
					ductSytemSet.strLineType = "默认";
				}
				Database.ExecuteCommand(string.Format("insert into Duct_SystemType (SystemTypeName,SytemTypeNameAbb,LineColor,LineType,LineWidth,SystemType,PressLevel,DuctMaterial) values ('{0:G}','{1:G}','{2:G}','{3:G}','{4:G}','{5:G}','{6:G}','{7:G}')", new object[]
				{
					ductSytemSet.strSystemName,
					ductSytemSet.strSystemTypeAbb,
					ductSytemSet.strLineColor,
					ductSytemSet.strLineType,
					ductSytemSet.strLineWidth,
					ductSytemSet.strSystemType,
					ductSytemSet.strPresslevel,
					ductSytemSet.strMateria
				}));
			}
		}

		public static DuctSytemSet GetSystemTypePara(string strSystemType)
		{
			DuctSytemSet ductSytemSet = new DuctSytemSet();
			DataTable dataTable = Database.ExecQuery("select *from Duct_SystemType  where SystemTypeName ='" + strSystemType + "'  order by id ");
			int num = 0;
			if (num < dataTable.Rows.Count)
			{
				ductSytemSet.strSystemName = dataTable.Rows[num]["SystemTypeName"].ToString();
				ductSytemSet.strSystemTypeAbb = dataTable.Rows[num]["SytemTypeNameAbb"].ToString();
				ductSytemSet.strLineColor = dataTable.Rows[num]["LineColor"].ToString();
				ductSytemSet.strLineType = dataTable.Rows[num]["LineType"].ToString();
				ductSytemSet.strLineWidth = dataTable.Rows[num]["LineColor"].ToString();
				ductSytemSet.strSystemType = dataTable.Rows[num]["SystemType"].ToString();
				ductSytemSet.strPresslevel = dataTable.Rows[num]["PressLevel"].ToString();
				ductSytemSet.strMateria = dataTable.Rows[num]["DuctMaterial"].ToString();
			}
			if (dataTable.Rows.Count == 0)
			{
				ductSytemSet.strPresslevel = "中低压";
				ductSytemSet.strMateria = "镀锌钢板";
			}
			return ductSytemSet;
		}

		public static List<System.Drawing.Color> GetColor(double value1, double value2)
		{
			List<System.Drawing.Color> list = new List<System.Drawing.Color>();
			list.Add(System.Drawing.Color.Black);
			list.Add(System.Drawing.Color.Black);
			try
			{
				string sqlStr = string.Format("select RGB from Duct_Compute where min<{0:G} and max>={1:G} and Type==1", value1, value1);
				string sqlStr2 = string.Format("select RGB from Duct_Compute where min<{0:G} and max>={1:G} and Type==2", value2, value2);
				DataTable dataTable = Database.ExecQuery(sqlStr);
				DataTable dataTable2 = Database.ExecQuery(sqlStr2);
				for (int i = 0; i < dataTable.Rows.Count; i++)
				{
					List<int> rgb = DuctCompute.GetRGB(dataTable.Rows[i]["RGB"].ToString());
					System.Drawing.Color value3 = System.Drawing.Color.FromArgb(rgb[0], rgb[1], rgb[2]);
					list[0] = value3;
				}
				for (int j = 0; j < dataTable2.Rows.Count; j++)
				{
					List<int> rgb2 = DuctCompute.GetRGB(dataTable2.Rows[j]["RGB"].ToString());
					System.Drawing.Color value4 = System.Drawing.Color.FromArgb(rgb2[0], rgb2[1], rgb2[2]);
					list[1] = value4;
				}
			}
			catch
			{
			}
			return list;
		}
	}
}
